package com.woniuxy.cardweb.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.carddao.model.ValidCard;
import com.woniuxy.cardservice.service.ValidCardService;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.MyJwtUtil;
import com.woniuxy.model.dto.ValidCardDto;
import com.woniuxy.model.form.AddValidCardForm;
import com.woniuxy.model.form.UpdateValidCardForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 有效期会员卡表 前端控制器
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Api(tags = {"有效期会员卡资源接口"})
@RestController
@RequestMapping("/validCard")
public class ValidCardController {
    @Autowired
    private ValidCardService validCardService;

    /**
     * 商家端新增有效期卡
     * @param form 新增有效期卡参数对象
     * @param token 商家登录token
     * @return
     */
    @ApiOperation("商家端新增有效期会员卡")
    @PostMapping("/add.venue")
    public Result<ValidCardDto> addValidCard(@RequestBody @Validated AddValidCardForm form,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        ValidCardDto dto = validCardService.addValidCard(form,venueId);
        return Result.success("商家端新增有效期卡成功",dto);
    }

    /**
     * 商家端禁用有效期会员卡
     * @param cardId
     * @param token 商家登录token
     * @return
     */
    @ApiOperation("商家端禁用有效期会员卡")
    @GetMapping("/disabled.venue")
    public Result disabledValidCard(Integer cardId,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        validCardService.disabledValidCard(cardId,venueId);
        return Result.success("商家端禁用有效期会员卡成功");
    }
    /**
     * 商家端启用有效期会员卡
     * @param cardId
     * @param token 商家登录token
     * @return
     */
    @ApiOperation("商家端启用有效期会员卡")
    @GetMapping("/enable.venue")
    public Result enableValidCard(Integer cardId,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        validCardService.enableValidCard(cardId,venueId);
        return Result.success("商家端启用有效期会员卡成功");
    }

    /**
     * 商家端修改有效期会员卡
     * @param form 修改会员的参数对象
     * @param token 商家端登录token
     * @return
     */
    @ApiOperation("商家端修改有效期会员卡")
    @PostMapping("/update.venue")
    public Result updateValidCard(@RequestBody @Validated UpdateValidCardForm form,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        validCardService.updateValidCard(form,venueId);
        return Result.success("商家端修改有效期会员卡信息成功");
    }

    /**
     * 根据id查找有效期会员卡信息
     * @param cardId 有效期会员卡id
     * @return
     */
    @ApiOperation("根据id查找有效期会员卡信息")
    @GetMapping("/findById")
    public Result<ValidCardDto> findById(Integer cardId){
        ValidCard validCard = validCardService.getById(cardId);
        ValidCardDto dto = BeanUtil.toBean(validCard, ValidCardDto.class);
        return Result.success("根据id查找有效期会员卡信息成功",dto);
    }

    /**
     * 根据商家id查找有效期会员卡信息
     * @param venueId
     * @return
     */
    @ApiOperation("根据商家id查找有效期会员卡信息")
    @GetMapping("/findByVenueId")
    public Result<List<ValidCardDto>> findByVenueId(Integer venueId){
        List<ValidCardDto> dtos = validCardService.findByVenueId(venueId);
        return Result.success("根据商家id查找有效期会员卡信息成功",dtos);
    }

    @ApiOperation("商家查看自家有效期会员卡信息")
    @GetMapping("/venueList.venue")
    public Result<List<ValidCardDto>> venueListValidCard(@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        List<ValidCardDto> dtos = validCardService.findByVenueId(venueId);
        return Result.success("商家查看自家有效期会员卡信息",dtos);
    }

}

